#==============================================================================
# [RGSS] Blink
# Version : 1.1
# Author : private/privateer
#==============================================================================
# State bisa melindungi user dari attack, skill, dan/atau item.
#==============================================================================
# CHANGELOG :
# Ver. 1.0 | Initial Release
# Ver. 1.1 | Skill & Item Option Added
#=======================================================================
module PSS_Blink
  # State yang akan otomatis diremove jika blink habis
  # Khusu auto-state armor jangan ditaruh di AUTO_REMOVE! Taruhlah di tempatnya.
  # AUTO_STATE
  AUTO_REMOVE = []
  # Auto-State dari armor yang blink_countnya akan terus di-update tiap battle selesai.
  AUTO_STATE = [13]
  # Blink Counter yang dikurangi saat diserang
  BLINK_REMOVE_ON_ATTACK = 1
  # Blink Counter yang dikurangi saat kena skill
  BLINK_REMOVE_ON_SKILL = 1
  # Blink Counter yang dikurangi saat kena item
  BLINK_REMOVE_ON_ITEM = 1
  # Blink juga akan meng-negate healing effect
  BLINK_AFFECT_HEALING = false
  # Message yang akan keluar jika blink bekerja
  NEGATE_MESSAGE = "Negated!"
  BLINK_STATES = {
  # STATE_ID => BLINK_COUNT
  13 => 2
  }
end
class Game_Battler
  attr_accessor :blink_count
  include PSS_Blink 
  alias pss_blink_initialize initialize
  def initialize
    pss_blink_initialize
    @blink_count = 0
  end
  alias pss_blink_attack_effect attack_effect
  def attack_effect(*args)
    if self.damage < 0 && BLINK_AFFECT_HEALING
      return pss_blink_attack_effect(*args)
    else
      check_battler_blink
      unless @blink_count == 0 or BLINK_REMOVE_ON_ATTACK <= 0
        @blink_count -= BLINK_REMOVE_ON_ATTACK
        self.critical = false
        self.damage = NEGATE_MESSAGE
        if @blink_count == 0
          for a in BLINK_STATES.keys
            if AUTO_REMOVE.include?(a)
              remove_state(a)
              return true
            end
          end
        end
      else
        return pss_blink_attack_effect(*args)
      end
    end
  end
  alias pss_blink_skill_effect skill_effect
  def skill_effect(*args)
    if self.damage < 0 && BLINK_AFFECT_HEALING
      return pss_blink_skill_effect(*args)
    else
      check_battler_blink
      unless @blink_count == 0 or BLINK_REMOVE_ON_SKILL <= 0
        @blink_count -= BLINK_REMOVE_ON_SKILL
        self.critical = false
        self.damage = NEGATE_MESSAGE
        if @blink_count == 0
          for b in BLINK_STATES.keys
            if AUTO_REMOVE.include?(b)
              remove_state(b)
              return true
            end
          end
        end
      else
        return pss_blink_skill_effect(*args)
      end
    end
  end
  alias pss_blink_item_effect item_effect
  def item_effect(*args)
    if recover_hp > 0 && BLINK_AFFECT_HEALING
      return pss_blink_item_effect(*args)
    else
      check_battler_blink
      unless @blink_count == 0 or BLINK_REMOVE_ON_ITEM <= 0
        @blink_count -= BLINK_REMOVE_ON_ITEM
        self.critical = false
        recover_hp = 0
        self.damage = NEGATE_MESSAGE
        if @blink_count == 0
          for c in BLINK_STATES.keys
            if AUTO_REMOVE.include?(c)
              remove_state(c)
              return true
            end
          end
        end
      else
        return pss_blink_item_effect(*args)
      end
    end
  end  
  alias pss_blink_add_state add_state
  def add_state(state_id, force = false)
    if BLINK_STATES.include?(state_id)
      @blink_count += BLINK_STATES[state_id]
    end
    return pss_blink_add_state(state_id, force = false)
  end
 
  alias pss_blink_remove_state remove_state
  def remove_state(state_id, force = false)
    if BLINK_STATES.include?(state_id)
      @blink_count -= BLINK_STATES[state_id]
      @blink_count = 0 if @blink_count < 0       
    end
    return pss_blink_remove_state(state_id, force = false)
  end
  def check_battler_blink
    return @blink_count
  end
end
class Scene_Battle
  alias pss_blink_start_phase5 start_phase5
  def start_phase5
    for actor in $game_party.actors
      if actor.blink_count == 0
        for j in PSS_Blink::AUTO_STATE&actor.states
          actor.blink_count += PSS_Blink::BLINK_STATES[j]
        end
      end
    end
    pss_blink_start_phase5
  end
end